
$(document).ready(function(){
	//初始化时间控件
	timeControl("applyTime");
	    laydate({
        elem: '#delay_time', //需显示日期的元素选择器
        event: 'focus', //触发事件
        format: 'YYYY-MM-DD hh:mm:ss', //日期格式
        istime: true, //是否开启时间选择
        isclear: true, //是否显示清空
        istoday: true, //是否显示今天
        issure: true, //是否显示确认
        festival: true, //是否显示节日
        min: '1900-01-01 00:00:00',//最小日期
        max: '2099-12-31 23:59:59',//最大日期
        start: laydate.now(),    //开始日期
        key: '2017-01-17 23:22:59'
        //choose: function(dates){} //选择好日期的回调
    });
	loanList("","","",1);
});

/**
 * 获取数据列表
 */
function loanList(time,params,orderStatus,current){
		$("#thislist").html("");
		$.ajax({
			url: urlcore + "/api/loanOrder/getLoanOrderList?time=" + time + "&param=" + params + "&orderStatus=" + orderStatus + "&current=" + current,
			type: "get",
			async:'false',
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				
        	if(data.success == true) {
        		var html ="";
					$.each(data.data.records, function(i, n) {
					var id = n.id;
					html+=  '<tr class="footable-even" style="display: table-row;">' +
							    '<td class="footable-visible"><input type="checkbox" value="'+n.id+'" name="selectcheck" /></td>' +
								'<td class="footable-visible">' + n.id + '</td>' +
								'<td class="footable-visible">' + n.orderNumber + '</td>' +
								'<td class="footable-visible">' + n.user.userName + '</td>' +
								'<td class="footable-visible">' + n.user.phone + '</td>' +
								'<td class="footable-visible">' + n.overdueDays + '</td>' +
								'<td class="footable-visible">' + n.limitDays + '</td>' +
								'<td class="footable-visible">' + n.borrowMoney + '</td>' +
								'<td class="footable-visible">' + n.realMoney + '</td>' +
								'<td class="footable-visible">' + n.needPayMoney + '</td>' +
								'<td class="footable-visible">' + n.giveTime + '</td>' +
								'<td class="footable-visible">' +  n.limitPayTime+ '</td>' +
								'<td class="footable-visible footable-last-column">&nbsp;'+
									'<a class="" name="催款" href="javascript:;" data-toggle="modal" data-target="#delay" onclick=\"delay('+n.userId+')\">延期</a>&nbsp;'+
									'<a class="" name="查看认证信息" href="javascript:;" data-toggle="modal" data-target="#deliver" onclick=\"deliver('+n.userId+')\" >转交</a>&nbsp;'+
									'<a class="" name="详情" href="loan_order_detail.html?id='+n.id+'" >详情</a>&nbsp;'+
									'<a class="" name="记录" href="loan_record.html?userId='+n.userId+'" >记录</a>&nbsp;'+
								'</tr>';
							
					});
					$('#thislist').append(html);
					$("#pager").pager({
						pagenumber: current,
						pagecount: data.data.pages,
						totalcount: data.data.total,
						buttonClickCallback: PageClick
					});
				} else if (data.code == 'OVERTIME'){
					var thisUrl = window.location.href;
					if (thisUrl.indexOf('login.html') <= -1) {
						top.window.location.href="login.html";
					}

				} 

			},
			error: function() {
				alert("服务器异常");
			}
		});
	}

PageClick = function(pageclickednumber) {
	var time=$("#applyTime").val();
	var param=$("#param").val();
	var orderStatus=$("#orderStatus").val();
	loanList(time,param,orderStatus,pageclickednumber)
}
	


function EnterSearchList() {
	var code = event.keyCode;
	if(code == 13) {
		searchList();
	}
}

/**
 * 搜索
 */
function searchList(){
	var time=$("#applyTime").val();
	var param=$("#param").val();
	var orderStatus=$("#orderStatus").val();
  loanList(time,param,orderStatus,1)
}


/**
 * 全选
 */
function selectAll(o){
	var mm=document.getElementsByName("selectcheck");
	for(var i=0;i<mm.length;i++){
		mm[i].checked=o.checked;
	}
}

/**
 * 延期弹框
 */
function delay(userId){
	document.getElementById("delay_time").setAttribute("thisId",userId);
}
/**
 * 弹框保存
 */
function delaytime(){
	var delay=document.getElementById("delay_time");
	var userId=delay.attributes.thisid.value;
	var time=delay.value;
	//查看该用户有没有在延期的记录
	var isdelay=true;
		$.ajax({
		url: urlcore + "/api/delay/selectOne?userId="+userId,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		data:{},
		success: function(data) {
			if(data.code == "FAIL") {
		swal({
			title: "提示!",
			text: "该用户已经处于延期中,将要替换延期时间？!？",
			type: "warning",
			showCancelButton: true,
			closeOnConfirm: false,
			confirmButtonText: "确定",
			confirmButtonColor: "#ec6c62"
		}, save(userId,time));
			
			} 
		}
	});
}

function save(userId,time){
	$.ajax({
		url: urlcore + "/api/delay/add?userId="+userId+"&delayTime="+time,
		type: "post",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		data:{},
		success: function(data) {
			if(data.success == true) {
			swal("Good!", "提交成功", "success");
			   window.location.reload();
			}  else {
				swal("OMG!", data.msg), "error");
			}
		}
	});
}

/**
 * 转交弹框
 */
function deliver(){
	document.getElementById("deliver").setAttribute("thisId",userId);
}

/**
 * 转交弹窗保存
 */
function devolve(){
	var delay=document.getElementById("deliver");
	var userId=delay.attributes.thisid.value;
	var time=delay.value;
	$.ajax({
		url: urlcore + "/api/delay/add?userId="+userId+"&delayTime="+time,
		type: "post",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		data:{},
		success: function(data) {
			if(data.success == true) {
				swal("Good!", "提交成功", "success");
			   window.location.reload();
			}  else {
				alert(data.msg);
			}
		}
	});
	
}
